package com.techsoul.module.tms.service.impl;
import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.techsoul.common.core.pojo.vo.UserVo;
import com.techsoul.common.core.util.ServletUtils;
import com.techsoul.common.core.util.id.IdWorker;
import com.techsoul.common.security.service.TokenService;
import com.techsoul.module.tms.mapper.TPoPurchaseOrderItemMapper;
import com.techsoul.module.tms.mapper.TPoPurchaseOrderMapper;
import com.techsoul.module.tms.pojo.entity.TPoPurchaseOrder;
import com.techsoul.module.tms.pojo.param.TFoFreightOrderSearchParam;
import com.techsoul.module.tms.pojo.param.TPoPurchaseOrderSaveParam;
import com.techsoul.module.tms.pojo.param.TPoPurchaseOrderSearchParam;
import com.techsoul.module.tms.pojo.vo.TFoFreightOrderVo;
import com.techsoul.module.tms.pojo.vo.TPoPurchaseOrderVo;
import com.techsoul.module.tms.service.ITPoPurchaseOrderItemService;
import com.techsoul.module.tms.service.ITPoPurchaseOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * 物流计划
 *
 * @author zhaowei
 * @since 2021-05-24 19:07:22
 */
@DS("tms")
@Service
@Slf4j
public class TPoPurchaseOrderServiceImpl  implements ITPoPurchaseOrderService {
    /**
     * 待派车
     */


    /**
     * 运输中
     */
    private static final String TRANSPORTING = "planned";

    /**
     * 已完成
     */
    private static final String COMPLETE = "complete";
    @Autowired
    private TPoPurchaseOrderMapper mapper;

    @Autowired
    private TPoPurchaseOrderItemMapper itemmapper;
    @Autowired
    private TokenService tokenService;

    @Autowired
    private IdWorker idWorker;


    @Override
    public String insert(TPoPurchaseOrderSaveParam params) {
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        TPoPurchaseOrder entity = new TPoPurchaseOrder();
        BeanUtils.copyProperties(params, entity);
        entity.setId(idWorker.nextId() + "").preInsert(user.getId());
        mapper.insert(entity);
        return entity.getId();
    }
    @Override
    public void delete(String id) {
        mapper.deleteById(id);
    }
    @Override
    public void update(String id, TPoPurchaseOrderSaveParam params) {
        TPoPurchaseOrder entity = new TPoPurchaseOrder();
        BeanUtils.copyProperties(params, entity);
        entity.setId(id);
        mapper.updateById(entity);
    }
    @Override
    public TPoPurchaseOrderVo findById(String id) {
        return mapper.findById(id);
    }
    @Override
    public List<TPoPurchaseOrder> findAll() {
        return mapper.selectList(new QueryWrapper<>());
    }
    @Override
    public List<TPoPurchaseOrderVo> findAll(TPoPurchaseOrderSearchParam params) {
        return mapper.findAll(params);
    }


    @Override
    public IPage<TPoPurchaseOrderVo> findAll(TPoPurchaseOrderSearchParam params, Integer current, Integer pageSize) {
        return this.mapper.findAll(new Page<>(current, pageSize), params);
    }


}
